//
//  VisitorView.swift
//  夏祖强的新浪微博
//
//  Created by 夏祖强 on 16/5/11.
//  Copyright © 2016年 夏祖强. All rights reserved.
//

import UIKit
import SnapKit

//访客视图协议
protocol VisitorViewDelegate : NSObjectProtocol{

    func vistiorViewDidRegister()
    
    func vistiorViewDidLogin()
    
}

//访客视图
class VisitorView: UIView {
    
    //MARK: - 代理属性
    weak var delegate :VisitorViewDelegate?
    
    //MARK: - 生命周期函数
    override init(frame : CGRect){
    super.init(frame: frame)
    setupUI()
        
        //添加监听方法
        registerButton.addTarget(self, action:#selector(VisitorView.clickRegisterButton), forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: #selector(VisitorView.clickLoginButton), forControlEvents: .TouchUpInside)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 监听方法
    @objc private func clickRegisterButton () {
        print("点击注册")
        delegate?.vistiorViewDidRegister()
    }
    @objc private func clickLoginButton() {
        print("点击登陆")
        delegate?.vistiorViewDidLogin()
    }
    //MARK: - 公共方法
    func setupInfo(imageName:String? = nil,message:String){
        
        messageLabel.text = message
        //如果图像为nil,表示首页
        guard let imageName = imageName else{
            startAnimation()
        return
        }
        //不是首页雪要隐藏房子
        homeIconView.hidden = true
        maskIconView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    //开始动画
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath :"transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //提示:对于需要循环播放的动画,可以设置此属性为false
        //视图离开频幕后,不会被销毁
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    //MARK: - 私有控件
    //图标视图
    private lazy var iconView :UIImageView = UIImageView(image :UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //遮罩视图 - 提示不要写 maskView, 父类有maskView属性
    private lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子视图
    private lazy var homeIconView :UIImageView = UIImageView(image :UIImage(named:"visitordiscover_feed_image_house"))
    
    //消息标签
    private lazy var messageLabel :UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人,回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()//颜色
        label.numberOfLines = 0 //换行
        label.font = UIFont.systemFontOfSize(14)//大小
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton : UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        return button
    }()
    //登陆按钮
    private lazy var loginButton :UIButton = {
        let button = UIButton()

        button.setTitle("登陆", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        return button
    }()
}
// MARK - 设置界面
extension VisitorView {
    private func setupUI(){
//    backgroundColor = UIColor.blueColor()
        //1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置文字对齐方式
        messageLabel.textAlignment = .Center
        //2.自动布局
        //图标视图
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-50)
        }
        //小房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        //提示文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224,height: 36))
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100,height: 36))
        }
        //登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        //遮罩视图
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton.snp_top)
        }
        //空白区域可以使用背景颜色(能够使用颜色的不要使用图片,占内存)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        /**for v in subviews{
        v.translatesAutoresizingMaskIntoConstraints = false
        }
        // VFL - 可视化格式语言,设置居中非常麻烦
        //苹果原生自动布局
        /**
         核心公式 view1.attr1 = view2.attr2 * multiplier + constant
         一个视图.属性 = 另外一个视图.属性*乘积 + 约束值
         
         约束是添加到公共的共有控件上
         
         使用制动布局时,尽量参照唯一控件布局,不要把所有约束都射知道父视图上
         */
        //图标视图
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //小房子视图
        self.addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //消息文字
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 20))
        //指定宽度和高度:没有参照对象
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
 */
        
    }
}